package com.mrjy.queries.bo;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.QuestionGoodInfo;
import com.mrjy.common.entity.QuestionNoticeInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.queries.dao.IQuestionCommentDao;
import com.mrjy.queries.dao.IQuestionGoodInfoDao;
import com.mrjy.queries.dao.IQuestionInfoDao;

@Service("questionNoticeBo")
public class QuestionNoticeBo implements IQuestionNoticeBo {

	@Resource
	private IQuestionCommentDao commentDao;
//	@Resource
//	private IQuestionInfoDao questionDao;
	@Resource
	private IQuestionGoodInfoDao goodDao;
	
	@Override
	public int countNoRead(UserInfo userInfo) {
		if (userInfo == null) {
			return 0;
		}
		int total = commentDao.countNoReadComment(userInfo.getId())+goodDao.countNoReadGood(userInfo.getId());
		return total;
	}
	
	@Override
	public Object queryNoReadNotice(QuestionNoticeInfo notice,
			UserInfo userInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		notice.setUserId(userInfo.getId());
		notice.setPage(1);
		notice.setRows(notice.getRows());
		List<QuestionNoticeInfo> list = commentDao.queryNoReadNotice(notice);
		int[] ids = {};
		int currentNum = 0;
		QuestionComment comment = new QuestionComment();
		comment.setText("评论");
		comment.setIds(ids);
		QuestionGoodInfo goodInfo = new QuestionGoodInfo();
		goodInfo.setText("点赞");
		goodInfo.setIds(ids);
		for (QuestionNoticeInfo n : list) {
			if (comment.getText().equals(n.getNoticeType())) {
				ids = comment.getIds();
				currentNum = ids.length;
				ids = Arrays.copyOf(ids, currentNum+1);
				ids[currentNum] = n.getId();
				comment.setIds(ids);
			}else if (goodInfo.getText().equals(n.getNoticeType())) {
				ids = goodInfo.getIds();
				currentNum = ids.length;
				ids = Arrays.copyOf(ids, currentNum+1);
				ids[currentNum] = n.getId();
				goodInfo.setIds(ids);
			}
		}
		if (goodInfo.getIds().length!=0) {
			goodDao.readGood(goodInfo);//点赞通知置为已读
		}
		if (comment.getIds().length!=0) {
			commentDao.readComment(comment);//评论通知置为已读
		}
		return list;
	}
	
	
	@Override
	public Object queryNotice(QuestionNoticeInfo notice,UserInfo userInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		notice.setPageStart(notice.getPageStart()+notice.getIds()[0]);//过滤掉从未读消息变为已读消息的消息
		notice.setUserId(userInfo.getId());
		List<QuestionNoticeInfo> list = commentDao.queryNotice(notice);
		return list;
	}

	
}
